package com.bong.panpan.bongcharttools.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bong.panpan.bongcharttools.R;
import com.bong.panpan.bongcharttools.fragment.ChartListContents;
import com.bong.panpan.bongcharttools.fragment.ChartListFragment.OnListFragmentInteractionListener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@SuppressWarnings("ResourceType")
public class MyChartDataRecyclerViewAdapter extends RecyclerView.Adapter<MyChartDataRecyclerViewAdapter.ViewHolder> implements View.OnClickListener {

    private final List<ChartListContents.ChartListItem> mValues;
    private final OnListFragmentInteractionListener mListener;

    public MyChartDataRecyclerViewAdapter(List<ChartListContents.ChartListItem> items, OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_chartdata, parent, false);
        return new ViewHolder(view);
    }

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.mIdView.setText("" + (position+1));
        holder.mContentView.setText(mValues.get(position).content);
        holder.openBtn.setId(position * 10);
        holder.deleteBtn.setId(position * 10 + 1);
        holder.shareBtn.setId(position * 10 + 2);
        holder.mContentDateView.setText(dateFormat.format(new Date(mValues.get(position).contentDate)));
        holder.shareBtn.setOnClickListener(this);
        holder.openBtn.setOnClickListener(this);
        holder.deleteBtn.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        if (null != mListener) {
            // Notify the active callbacks interface (the activity, if the
            // fragment is attached to one) that an item has been selected.
            mListener.onListFragmentInteraction(mValues.get(v.getId()/10),v.getId());
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView mContentView, mContentDateView;
        public final ImageButton shareBtn,openBtn, deleteBtn;
        public ChartListContents.ChartListItem mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = (TextView) view.findViewById(R.id.id);
            mContentView = (TextView) view.findViewById(R.id.content);
            mContentDateView = (TextView) view.findViewById(R.id.content_date);
            shareBtn = (ImageButton) view.findViewById(R.id.share_btn);
            openBtn = (ImageButton) view.findViewById(R.id.open_btn);
            deleteBtn = (ImageButton) view.findViewById(R.id.delete_btn);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }
    }
}
